package com.fenshi.manager.activity;

import android.app.ProgressDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fenshi.manager.R;
import com.fenshi.manager.adapter.ListViewAdapter2;
import com.fenshi.presenter.ParkPresenter;
import com.fenshi.presenter.imp.ParkPresenterImp;
import com.fenshi.until.CookicUntil;
import com.fenshi.view.ParkView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;
import com.quickdv.until.ViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CarListManagerActivity extends BaseActivity implements ParkView {
    List<Map<String, Object>> data;
    ProgressDialog loading;
    ListViewAdapter2 adapter;
    ListView list;
    ParkPresenter presenter;

    String type = "1,2,3,4,5,6,7";

    @Override
    public int getLayoutID() {
        return R.layout.activity_car_list;
    }

    @Override
    public void initView() {
        AlertHelper helper = new AlertHelper(this);
        loading = helper.LoadingAlert();

        list = findView(R.id.carlist);
        adapter = new ListViewAdapter2(this, data, R.layout.activity_park_manager_item, new String[]{"carname", "carrying",  "course"}, new int[]{R.id.park_car_name, R.id.park_car_carry_value,  R.id.park_car_mileage_value}) {
            @Override
            protected void renderData(int position, View convertView) {
                super.renderData(position, convertView);
                ImageView view = ViewHolder.get(convertView, R.id.park_car_pic);
                if (data.get(position).get("pic") != null) {
                    String obj = (String) data.get(position).get("pic");

                    Picasso.with(c).load(CookicUntil.IMG_URL + obj).centerCrop().resize(150, 120).error(R.drawable.loadfailed).placeholder(R.drawable.loading).into(view);
                }

                TextView price = ViewHolder.get(convertView,R.id.park_car_price_value);
                float time =Math.round((Float.valueOf((String)data.get(position).get("basictariff"))) * 100);
                price.setText( time/100.0 + "元/分钟");
            }
        };
        list.setAdapter(adapter);

        presenter.getRestCar(type);
    }

    @Override
    public void initActionbar() {
        getSupportActionBar().setTitle("可租用车辆管理");
    }

    @Override
    public void initData() {
        presenter = new ParkPresenterImp(this);
        data = new ArrayList<>();
    }


    @Override
    public void dismissLoad() {
        if (loading != null && loading.isShowing()) loading.dismiss();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        if (loading != null && !loading.isShowing()) loading.show();
    }

    @Override
    public void carList(List<Map<String, Object>> car, boolean isRefresh) {
        if (isRefresh) data.clear();
        data.addAll(car);
    }

    @Override
    public void notifyDateChange() {
        if (adapter != null) adapter.notifyDataSetChanged();
    }


}
